package com.leyou.auth.controller;

import com.leyou.auth.service.IauthService;
import com.leyou.common.auth.entity.UserInfo;
import com.leyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiuer
 * @description 授权中心控制层
 * @create 2019/7/8 22:24
 */
@RestController
public class AuthController {
    @Autowired
    private IauthService authService;

    /**
     * 验证用户登陆并颁发token
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Void> login(@RequestParam("username") String username,
                                      @RequestParam("password") String password, HttpServletResponse response) {
        authService.login(username, password, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 校验用户是否登陆
     * http://api.leyou.com/api/auth/verify
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verifyUser(HttpServletRequest request, HttpServletResponse response) {
        return ResponseEntity.ok(authService.verifyUser(request, response));
    }

    /**
     * 注销登陆
     * http://api.leyou.com/api/auth/logout
     */
    @PostMapping("logout")
    public ResponseEntity<Void> loginOut(HttpServletRequest request, HttpServletResponse response) {
        authService.loginOut(request, response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 微服务授权令牌
     *
     * @param id     微服务id
     * @param secret 微服务注册密码
     * @Return 返回令牌
     */
    @GetMapping("authorization")
    public ResponseEntity<String> authorize(@RequestParam("id") Long id, @RequestParam("secret") String secret) {
        return ResponseEntity.ok(authService.authorize(id,secret));

    }
}
